package fr.dauphine.spring.ctl;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import fr.dauphine.spring.bo.Client;
import fr.dauphine.spring.dao.ClientDAO;

@Controller
@RequestMapping("photo.html")
public class PhotoController {

	@Autowired
	private ClientDAO clientDAO;

	@RequestMapping(method = RequestMethod.GET)
	public void affichage(@RequestParam("idClient") String idClient, HttpServletResponse response) {
		try {
			Client client = clientDAO.getWithData(idClient);
			// force le rafraichissement du cache
			response.setHeader("Expires", "0");
			response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
			response.setHeader("Pragma", "public");

			response.setContentType(client.getPhotoType());
			response.setContentLength(client.getPhoto().length);

			// On remplit le flux de sortie
			ServletOutputStream outputStream = response.getOutputStream();
			outputStream.write(client.getPhoto());
			outputStream.flush();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}
